package cn.edu.swpu.cins.u_chain.frontend.service.user.Impl;

import cn.edu.swpu.cins.u_chain.Exception.user.StudentException;
import cn.edu.swpu.cins.u_chain.frontend.dao.user.StudentDao;
import cn.edu.swpu.cins.u_chain.frontend.dao.user.UserDao;
import cn.edu.swpu.cins.u_chain.frontend.entity.dto.user.UpdateRole;
import cn.edu.swpu.cins.u_chain.frontend.entity.dto.user.student.SkillList;
import cn.edu.swpu.cins.u_chain.frontend.entity.dto.user.student.StudentDetail;
import cn.edu.swpu.cins.u_chain.frontend.entity.dto.user.student.StudentInfo;
import cn.edu.swpu.cins.u_chain.frontend.entity.persistence.user.student.DegreeMajorGradeInfo;
import cn.edu.swpu.cins.u_chain.frontend.entity.persistence.user.LocationInfo;
import cn.edu.swpu.cins.u_chain.frontend.entity.persistence.user.student.SkillInfo;
import cn.edu.swpu.cins.u_chain.frontend.entity.persistence.user.student.StudentInfoDB;
import cn.edu.swpu.cins.u_chain.frontend.enums.UserRole;
import cn.edu.swpu.cins.u_chain.frontend.service.user.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentDao studentDao;
    @Autowired
    private UserDao userDao;

    /**
     * studentInfo 里面是所有的信息 DTO类 需要拆分 还是比较复杂
     * 学生添加个人信息
     * @param studentInfo
     * @return
     */
    @Override
    @Transactional(rollbackFor = {RuntimeException.class,StudentException.class,SQLException.class})
    public int addStudentInfo(StudentInfo studentInfo) {
        //思路是 将信息插入 获取主键ID
        DegreeMajorGradeInfo degreeMajorGradeInfo = new DegreeMajorGradeInfo(studentInfo);
        studentDao.addDegreeMajorGradeInfo(degreeMajorGradeInfo);
        int degreeId = degreeMajorGradeInfo.getDegreeMajorGradeId();
        int userId = studentInfo.getUserId();
        StudentInfoDB studentInfoDB = new StudentInfoDB(studentInfo);
        studentInfoDB.setDegreeMajorGradeId(degreeId);
        //插入数据库
        studentDao.addStudentInfo(studentInfoDB);
        //修改基本表中的角色状态
        return userDao.updateRole(new UpdateRole(userId, UserRole.STUDENT.getRole()));
    }

    @Override
    public int addSkillLists(List<SkillInfo> skillList) {
        return studentDao.addSkillInfos(skillList);
    }

    @Override
    public StudentDetail selectDetail(int userId) {
        return studentDao.selectStudentDetail(userId);
    }

    @Override
    public SkillList selectSkills(int userId) {
        return studentDao.selectSkills(userId);
    }
}
